import urllib2
import urllib
import sys

try: import json #python 2.6 included simplejson as json
except ImportError: import simplejson as json

def doGet(build, chr, start, end):
    url = "http://vis.systemsbiology.net/chromosome-index-svc/indexes/" + build + "/" + chr + "/" + start + "/" + end

    request = urllib2.Request(url)
    resp = urllib2.urlopen(request)
    
    output = resp.read()
    try:
        j = json.loads(output)

        genelist = []
        for item in j["items"]:
            genelist.append(item["NAME2"])

        for geneitem in sorted(set(genelist)):
            print geneitem

    except:
        fsock = open("exception.out", 'w')
        sys.stdout = fsock
        print output
        fsock.close()

if __name__ == "__main__":
    doGet(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
